מבוא למדעי המחשב הרצאה 2: עצי חיפוש בינאריים תזכורת: עץבינארי בנוסףלרשימהמקושרת ומערך, הצגנומבנהנתונים קונקרטיחדש עץבינארי עץבינארימורכבמ: שורש תת-עץשמאלי תת-עץימני A B C D E F G 2 תזכורת: שורש ותתי-עצים left subtree root right subtree עץ בינארי לא ריק ניתן לחלק לשורש ולשני תתי-עצים משימה בסיסית של מחשב נרצהלנהלקבוצת איבריםסדורה, כך שניתן יהיה להוסיף, לחפש, למצוא איבר מינימאלי ולהוציא איבר בזמן מהיר איזה מבנה נתונים הכי מתאים לביצוע המשימה? מערך ממוין? רשימה מקושרת? עץ בינארי? זמני ריצה עץחיפושבינאריהואעץחיפושאשרבסריקת InOrder מוצגיםאיבריולפיהסדרהנכון 20 6 עץ בינארי לעומת עץ חיפוש בינארי מערכיםממוינים : מציאתמנימום: מעברעלאיבריחיד חיפוש: מעברעללכלהיותר log n איברים הכנסה: מעברעללכלהיותר n איברים רשימות מקושרותלאממוינות \ ממוינות: מציאתמנימום: מעברעל n איבריםלכלהיותר \ איבריחיד חיפוש: מעברעל n איבריםלכלהיותר (בשניהמקרים) הכנסה: מעברעלאיבריחיד \ n איבריםלכלהיותר עציחיפושבינאריים (בגובהh ): מציאתמנימום: מעברעל h איבריםלכלהיותר חיפוש: מעברעל h איבריםלכלהיותר הכנסה: מעברעל h איבריםלכלהיותר
הרעיון של עץ חיפוש בינארי דוגמאות לעץ חיפוש בינארי 2 2 כלהאיבריםבתתהעץהשמאליקטנים מהאיברבשורש, וכלהאיבריםבתתהעץ הימניגדוליםמהאיברבשורש תנאיםאלומתקיימיםגםביחסלשורשהעץ כולו, וגםבכלאחדמתתיהעציםשלהעץ זהלאעץחיפושבינארי עציחיפושבינאריים 8 הכנסת איבר לעץ חיפוש בינארי דוגמת בנייה של עץ חיפוש בינארי כאשרמוכנסאיבר, ערכומושווהעםהשורש אםערכוקטןמערךהשורש, הואמוכנס באופןרקורסיבילתתהעץהשמאלי אםערכוגדולמערךהשורש, הואמוכנס באופןרקורסיבילתתהעץהימני במקרהשלשוויון, בחרנובמימושזהלא להכניסאתהאיבר נניח כי נרצה להכניס את האיברים הבאים לעץ חיפוש בינארי:,,,,, 8, 9 דוגמת בנייה של עץ חיפוש בינארי נניח כי נרצה להכניס את האיברים הבאים לעץ חיפוש בינארי:,,,,, 8, איך נממש? Binary Search Trees האםניתןלהשתמשבקודשל BinaryTree ואם כן איך? נכון! ע"יהורשה 8 8 2 2
כיצד נשווה בין איברי העץ? עלמנתלהחזיקאתהאיבריםבעץ באופןממויןעלינולדאוג שאיבריםיהיובעלי השוואהלכןנשתמשבממשק - Comparable (תזכורתלממשק): המחלקהBinarySearchNode public class BinarySearchNode extends BinaryNode { public BinarySearchNode (Comparable data) {super(data); public void insert(object toadd) { if (!(toadd instanceof Comparable)) throw new RuntimeException("Only a Comparable data may be inserted!"); else insert((comparable) toadd); public boolean contains(object tofind) { if (!(tofind instanceof Comparable)) return false; else return contains((comparable) tofind); public interface Comparable { public int compareto(object other); המחלקהBinarySearchNode המחלקהBinarySearchNode private boolean contains(comparable tofind) { int compareresult = tofind.compareto(data); if (compareresult == 0) return true; else if (compareresult < 0){ return (left!= null && left.contains(tofind)); else return (right!= null && right.contains(tofind)); private void insert(comparable toadd) { if (toadd.compareto(data) < 0){ if (left == null) left = new BinarySearchNode (toadd); else left.insert(toadd); else if (toadd.compareto(data) > 0){ if (right == null) right = new BinarySearchNode (toadd); else right.insert(toadd); הדגמהשלפעולת contains הדגמהשלפעולת contains 8 < go left 8
הדגמהשלפעולת contains הדגמהשלפעולת contains 8 > go right 8 > go right But right == null return false 20 9 מציאת מינימום בעץ מחיקת איבר מהעץ נרצהלהוציאאיברכלשהומהעץ (אםהאיבר נמצאבעץ) נטפלבשלושהמקרים: מקרהראשון: האיברנמצאבעלה מקרהשני: לצומתשלהאיברישבןאחד מקרהשלישי: לצומתשלהאיבריששניבנים public Object findmin(){ BinaryNode currnode = this; while (currnode.left!= null){ currnode = currnode.left; return currnode.data; 22 2 מחיקתאיברמהעץ מקרה ראשון מחיקתאיברמהעץ מקרה שני לצומתהמכילאתהאיברישבןאחד נניחשרוציםלהורידאתהאיבר הצומתהמכילאתהאיברהואעלה נניחשרוציםלהורידאתהאיבר פשוטנהפוךאתהמצביעשלהאבשל להיות null פשוט "נעלה" אתתתהעץשלהבןהיחידשל 2 2
public BinarySearchNode remove(comparable toremove){ if (toremove.compareto(data) < 0){ if (left!= null) left = ((BSN) left).remove(toremove); else if (toremove.compareto(data) > 0){ if (right!= null) right = ((BSN) right).remove(toremove); else{//need to remove the data in this node if (left == null right == null){ // the base cases... if (left == null) return right; else return left; else{ // this node has two children data = ((BSN) right).findmin(); right = ((BSN) right).remove((comparable) data); return this; //remove 26 מחיקתאיברמהעץ מקרהשלישי לצומתהמכילאתהאיבריששניבנים נניחשרוציםלהורידאתהאיבר נעתיקאתהמידעמהאיברהמינימאליבתת-העץ הימנישלהצומתהמכילאת () 20 נמחקאתהאיברהמינימאלי בתתהעץהימני, באופן רקורסיבי 2 public interface Comparator { int compare(object obj, Object obj2); // comparator 28 לעיתיםאנומעונייניםלהשוותביןשניאוביקטים לפיכמההשוואותנפרדות, לדוגמהסטודנטיםעפ"י מספרת.ז. שםמשפחה ציונים מהנעשה? נצמיד "שופט" למחלקה שיודע "להכריע" מי מביןשניעצמיםמאותהמחלקה יותרגדול 2 public class StudentComparatorByID implements Comparator { public int compare(object obj, Object obj2){ double id= ((Student) o).getid(); double id2= ((Student) o2).getid(); return id - id2; // StudentComparatorByID public class StudentComparatorByName implements Comparator { public int compare(object obj, Object obj2){ String name= ((Student) o).getname(); String name2= ((Student) o2).getname(); return name.compareto(name2); // StudentComparatorByName 0 29
public class StudentComparatorByNumOfPoints implements Comparator { public int compare(object obj, Object obj2){ doube nump= ((Student) o).getpoints(); double nump2= ((Student) o2).getpoints(); return nump - nump2; // StudentComparatorByNumOfPoints public class StudentComparatorByGrade implements Comparator { public int compare(object obj, Object obj2){ double grade= ((Student) o).getavggrade(); double grade2= ((Student) o2).getavegrade(); return grade - grade2; // StudentComparatorByGrade 2 Back to Binary Search Trees איךנממש? האםניתןלהשתמשבקודשל BinaryTree I think I have a DeJaVu! ואםכןאיך? נכון! שובע"יהורשה public static void insertionsort(object [] arr, Comparator comp){ for(int i=; i < arr.length; i = i + ){ int j=i; while(j>0 && (comp.compare(arr[i], arr[j-])<0){ arr[j] = arr[j-]; j=j -; BinarySearchTree public class BinarySearchNode extends BinaryNode{ private Comparator comp; public BinarySearchNode(Object data, Comparator comp) { super(data); this.comp = comp; // BinarySearchNode // (override insert, remove, etc.) // class BinarySearchNode BinarySearchTree import java.util.comparator; public class BinarySearchTree extends BinaryTree{ private Comparator comp; public BinarySearchTree(Comparator comp){ super(); this.comp = comp; // BinarySearchTree 6 6
CharacterComparator IntegerComparator public class CharacterComparator implements Comparator { public int compare(object o, Object o2) { return ((Character)o).charValue() - ((Character)o2).charValue()) ; public class IntegerComparator implements Comparator { public int compare(object o, Object o2) { return ((Integer)o).intValue() - ((Integer)o2).intValue() 8 הכנסה איבר חדש לעץ במחלקה BinarySearchTree public void insert(object toadd){ if (isempty()) { root = new BSN(toAdd, this.comp); else { root.insert(toadd); // insert 0 Binary search Tree import java.util.comparator; public class Main { public static void main(string[] args) { Comparator comp = new IntegerComparator(); BinarySearchTree tree = new BinarySearchTree(comp); tree.insert(new Integer(2)); tree.insert(new Integer()); tree.insert(new Integer()); tree.insert(new Integer()); 9 הכנסה איבר חדש לעץ תכונות של עץ חיפוש בינארי ההכנסה\הוצאה היא מקומית, כלומר אין צורךלהזיז את כל שאר האיברים עץ חיפוש בינארי הוא עץ בינארי לכל דבר (מבחינתגובה, גודלוכו') הזמן הדרוש לביצוע הפעולות פרופורציוני לגובה העץ, ולא למספר האיברים בעץ (כפי שקורה במערכים ורשימות). כאשר העץ מאוזן, גובהובערך.logn 2 במחלקה BinarySearchNode public void insert(object toadd) { if (comp.compare(toadd, this.data) < 0) if (left == null) left = new BSN(toAdd,this.comp); else left.insert(toadd); if (comp.compare(toadd, this.data) > 0) { if (right == null) right = new BSN(toAdd,this.comp); else right.insert(toadd); // insert